home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / rpclib / auth_unix.c < prev    next >
C/C++ Source or Header  |  1994-03-09  |  9KB  |  337 lines

  1. /*
  2.  * $Id: auth_unix.c,v 1.3 1994/03/09 01:34:07 jraja Exp $
  3.  *
  4.  * $Log: auth_unix.c,v $
  5.  * Revision 1.3  1994/03/09  01:34:07  jraja
  6.  * Changed the uids and gids to use uid_t and gid_t types.
  7.  *
  8.  * Revision 1.2  1993/11/10  01:16:59  jraja
  9.  * ANSI prototypes. Fixed includes.
  10.  * Changed authunix_validate()'s second argument from structure to
  11.  * structure pointer (correct now).
  12.  * Disabled the authunix_create_default() for AMITCP (for now).
  13.  *
  14.  */
  15. /* @(#)auth_unix.c    2.2 88/08/01 4.0 RPCSRC */
  16. /*
  17.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  18.  * unrestricted use provided that this legend is included on all tape
  19.  * media and as a part of the software program in whole or part.  Users
  20.  * may copy or modify Sun RPC without charge, but are not authorized
  21.  * to license or distribute it to anyone else except as part of a product or
  22.  * program developed by the user.
  23.  * 
  24.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  25.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  26.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  27.  * 
  28.  * Sun RPC is provided with no support and without any obligation on the
  29.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  30.  * modification or enhancement.
  31.  *
  32.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  33.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  34.  * OR ANY PART THEREOF.
  35.  * 
  36.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  37.  * or profits or other special, indirect and consequential damages, even if
  38.  * Sun has been advised of the possibility of such damages.
  39.  * 
  40.  * Sun Microsystems, Inc.
  41.  * 2550 Garcia Avenue
  42.  * Mountain View, California  94043
  43.  */
  44. #if !defined(lint) && defined(SCCSIDS)
  45. static char sccsid[] = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro";
  46. #endif
  47.  
  48. /*
  49.  * auth_unix.c, Implements UNIX style authentication parameters. 
  50.  *  
  51.  * Copyright (C) 1984, Sun Microsystems, Inc.
  52.  *
  53.  * The system is very weak.  The client uses no encryption for it's
  54.  * credentials and only sends null verifiers.  The server sends backs
  55.  * null verifiers or optionally a verifier that suggests a new short hand
  56.  * for the credentials.
  57.  *
  58.  */
  59.  
  60. #include <sys/param.h>
  61. #include <stdio.h>
  62. #include <stdlib.h>
  63. #include <rpc/types.h>
  64. #include <rpc/xdr.h>
  65. #include <rpc/auth.h>
  66. #include <rpc/auth_unix.h>
  67.  
  68. #ifdef AMITCP
  69. #include <bsdsocket.h>
  70. #endif
  71.  
  72. /*
  73.  * Unix authenticator operations vector
  74.  */
  75. static void    authunix_nextverf(AUTH * auth);
  76. static bool_t    authunix_marshal(AUTH * auth, XDR * xdrs);
  77. static bool_t    authunix_validate(AUTH * auth, struct opaque_auth * verfp);
  78. static bool_t    authunix_refresh(AUTH * auth);
  79. static void    authunix_destroy(AUTH * auth);
  80.  
  81. static struct auth_ops auth_unix_ops = {
  82.     authunix_nextverf,
  83.     authunix_marshal,
  84.     authunix_validate,
  85.     authunix_refresh,
  86.     authunix_destroy
  87. };
  88.  
  89. /*
  90.  * This struct is pointed to by the ah_private field of an auth_handle.
  91.  */
  92. struct audata {
  93.     struct opaque_auth    au_origcred;    /* original credentials */
  94.     struct opaque_auth    au_shcred;    /* short hand cred */
  95.     u_long            au_shfaults;    /* short hand cache faults */
  96.     char            au_marshed[MAX_AUTH_BYTES];
  97.     u_int            au_mpos;    /* xdr pos at end of marshed */
  98. };
  99. #define    AUTH_PRIVATE(auth)    ((struct audata *)auth->ah_private)
  100.  
  101. static void marshal_new_auth(AUTH *);
  102.  
  103.  
  104. /*
  105.  * Create a unix style authenticator.
  106.  * Returns an auth handle with the given stuff in it.
  107.  */
  108. AUTH *
  109. authunix_create(machname, uid, gid, len, aup_gids)
  110.     char *machname;
  111.     uid_t uid;
  112.     gid_t gid;
  113.     register int len;
  114.     gid_t *aup_gids;
  115. {
  116.     struct authunix_parms aup;
  117.     char mymem[MAX_AUTH_BYTES];
  118.     struct timeval now;
  119.     XDR xdrs;
  120.     register AUTH *auth;
  121.     register struct audata *au;
  122.  
  123.     /*
  124.      * Allocate and set up auth handle
  125.      */
  126.     auth = (AUTH *)mem_alloc(sizeof(*auth));
  127. #ifndef KERNEL
  128.     if (auth == NULL) {
  129.         (void)fprintf(stderr, "authunix_create: out of memory\n");
  130.         return (NULL);
  131.     }
  132. #endif
  133.     au = (struct audata *)mem_alloc(sizeof(*au));
  134. #ifndef KERNEL
  135.     if (au == NULL) {
  136.         (void)fprintf(stderr, "authunix_create: out of memory\n");
  137.         return (NULL);
  138.     }
  139. #endif
  140.     auth->ah_ops = &auth_unix_ops;
  141.     auth->ah_private = (caddr_t)au;
  142.     auth->ah_verf = au->au_shcred = _null_auth;
  143.     au->au_shfaults = 0;
  144.  
  145.     /*
  146.      * fill in param struct from the given params
  147.      */
  148.     (void)gettimeofday(&now,  (struct timezone *)0);
  149.     aup.aup_time = now.tv_sec;
  150.     aup.aup_machname = machname;
  151.     aup.aup_uid = uid;
  152.     aup.aup_gid = gid;
  153.     aup.aup_len = (u_int)len;
  154.     aup.aup_gids = aup_gids;
  155.  
  156.     /*
  157.      * Serialize the parameters into origcred
  158.      */
  159.     xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
  160.     if (! xdr_authunix_parms(&xdrs, &aup)) 
  161.         exit(3);
  162.     au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
  163.     au->au_origcred.oa_flavor = AUTH_UNIX;
  164. #ifdef KERNEL
  165.     au->au_origcred.oa_base = mem_alloc((u_int) len);
  166. #else
  167.     if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) {
  168.         (void)fprintf(stderr, "authunix_create: out of memory\n");
  169.         return (NULL);
  170.     }
  171. #endif
  172.     bcopy(mymem, au->au_origcred.oa_base, (u_int)len);
  173.  
  174.     /*
  175.      * set auth handle to reflect new cred.
  176.      */
  177.     auth->ah_cred = au->au_origcred;
  178.     marshal_new_auth(auth);
  179.     return (auth);
  180. }
  181.  
  182. /*
  183.  * Returns an auth handle with parameters determined by doing lots of
  184.  * syscalls.
  185.  */
  186. AUTH *
  187. authunix_create_default(void)
  188. {
  189.     register int len;
  190.     char machname[MAX_MACHINE_NAME + 1];
  191.     register uid_t uid;
  192.     register gid_t gid;
  193.     gid_t gids[NGRPS];
  194.  
  195.     if (gethostname(machname, MAX_MACHINE_NAME) == -1)
  196.         exit(3);
  197.     machname[MAX_MACHINE_NAME] = 0;
  198.     uid = geteuid();
  199.     gid = getegid();
  200.     if ((len = getgroups(NGRPS, gids)) < 0)
  201.         abort();
  202.     return (authunix_create(machname, uid, gid, len, gids));
  203. }
  204.  
  205. /*
  206.  * authunix operations
  207.  */
  208.  
  209. static void
  210. authunix_nextverf(auth)
  211.     AUTH *auth;
  212. {
  213.     /* no action necessary */
  214. }
  215.  
  216. static bool_t
  217. authunix_marshal(auth, xdrs)
  218.     AUTH *auth;
  219.     XDR *xdrs;
  220. {
  221.     register struct audata *au = AUTH_PRIVATE(auth);
  222.  
  223.     return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
  224. }
  225.  
  226. static bool_t
  227. authunix_validate(AUTH *auth, struct opaque_auth *verfp)
  228. {
  229.     register struct audata *au;
  230.     XDR xdrs;
  231.  
  232.     if (verfp->oa_flavor == AUTH_SHORT) {
  233.         au = AUTH_PRIVATE(auth);
  234.         xdrmem_create(&xdrs, verfp->oa_base, verfp->oa_length, XDR_DECODE);
  235.  
  236.         if (au->au_shcred.oa_base != NULL) {
  237.             mem_free(au->au_shcred.oa_base,
  238.                 au->au_shcred.oa_length);
  239.             au->au_shcred.oa_base = NULL;
  240.         }
  241.         if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
  242.             auth->ah_cred = au->au_shcred;
  243.         } else {
  244.             xdrs.x_op = XDR_FREE;
  245.             (void)xdr_opaque_auth(&xdrs, &au->au_shcred);
  246.             au->au_shcred.oa_base = NULL;
  247.             auth->ah_cred = au->au_origcred;
  248.         }
  249.         marshal_new_auth(auth);
  250.     }
  251.     return (TRUE);
  252. }
  253.  
  254. static bool_t
  255. authunix_refresh(auth)
  256.     register AUTH *auth;
  257. {
  258.     register struct audata *au = AUTH_PRIVATE(auth);
  259.     struct authunix_parms aup;
  260.     struct timeval now;
  261.     XDR xdrs;
  262.     register int stat;
  263.  
  264.     if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
  265.         /* there is no hope.  Punt */
  266.         return (FALSE);
  267.     }
  268.     au->au_shfaults ++;
  269.  
  270.     /* first deserialize the creds back into a struct authunix_parms */
  271.     aup.aup_machname = NULL;
  272.     aup.aup_gids = (gid_t *)NULL;
  273.     xdrmem_create(&xdrs, au->au_origcred.oa_base,
  274.         au->au_origcred.oa_length, XDR_DECODE);
  275.     stat = xdr_authunix_parms(&xdrs, &aup);
  276.     if (! stat) 
  277.         goto done;
  278.  
  279.     /* update the time and serialize in place */
  280.     (void)gettimeofday(&now, (struct timezone *)0);
  281.     aup.aup_time = now.tv_sec;
  282.     xdrs.x_op = XDR_ENCODE;
  283.     XDR_SETPOS(&xdrs, 0);
  284.     stat = xdr_authunix_parms(&xdrs, &aup);
  285.     if (! stat)
  286.         goto done;
  287.     auth->ah_cred = au->au_origcred;
  288.     marshal_new_auth(auth);
  289. done:
  290.     /* free the struct authunix_parms created by deserializing */
  291.     xdrs.x_op = XDR_FREE;
  292.     (void)xdr_authunix_parms(&xdrs, &aup);
  293.     XDR_DESTROY(&xdrs);
  294.     return (stat);
  295. }
  296.  
  297. static void
  298. authunix_destroy(auth)
  299.     register AUTH *auth;
  300. {
  301.     register struct audata *au = AUTH_PRIVATE(auth);
  302.  
  303.     mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
  304.  
  305.     if (au->au_shcred.oa_base != NULL)
  306.         mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
  307.  
  308.     mem_free(auth->ah_private, sizeof(struct audata));
  309.  
  310.     if (auth->ah_verf.oa_base != NULL)
  311.         mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
  312.  
  313.     mem_free((caddr_t)auth, sizeof(*auth));
  314. }
  315.  
  316. /*
  317.  * Marshals (pre-serializes) an auth struct.
  318.  * sets private data, au_marshed and au_mpos
  319.  */
  320. static void
  321. marshal_new_auth(auth)
  322.     register AUTH *auth;
  323. {
  324.     XDR        xdr_stream;
  325.     register XDR    *xdrs = &xdr_stream;
  326.     register struct audata *au = AUTH_PRIVATE(auth);
  327.  
  328.     xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
  329.     if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
  330.         (! xdr_opaque_auth(xdrs, &(auth->ah_verf)))) {
  331.         perror("auth_none.c - Fatal marshalling problem");
  332.     } else {
  333.         au->au_mpos = XDR_GETPOS(xdrs);
  334.     }
  335.     XDR_DESTROY(xdrs);
  336. }
  337.